<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HEAD>
<TITLE>知恵袋 q12114911402   javascriptでcookieに記憶と呼び出しをさせたい </TITLE>
<base href="http://kiyoto777.web.fc2.com/test/" />
<script type="text/javascript">
function show_block(){
 if(document.getElementById("box").style.display == "block"){
  document.getElementById("box").style.display   =  "none";
 }else{
  document.getElementById("box").style.display   =  "block";
 }
}
function cookie_read(cookie_keys){
 var cookie_status = 9;                                    // return値
 var cookieName    = cookie_keys + '=';                    // cookie名のセット
 var cookies       = document.cookie;                      // cookieの読み込み
 var positions     = cookies.indexOf(cookieName);          // cookieが存在のチェック
 if(positions!=-1){                                        // cookieが存在するか？
  var start_pos    = positions + cookieName.length;        // start position
  var end_pos      = cookies.indexOf( ';', start_pos );    // end   position
  if( end_pos      == -1 ){
   end_pos         = cookies.length;
  }
  var cookie_value = decodeURIComponent(cookies.substring(start_pos,end_pos));// 値を得る
  cookie_status    = 0;
 }
 return {cookie_status:cookie_status,cookie_value:cookie_value};
}
function cookie_write(cookie_keys,cookie_values,cookie_expires){
 var expire_date = new Date();
 expire_date.setTime(expire_date.getTime() + cookie_expires*24*60*60*1000);
 document.cookie = cookie_keys+'='+encodeURIComponent(cookie_values)+'; expires='+expire_date.toGMTString();
}
function cookie_delete(cookie_keys){
 var expire_date = new Date();
 expire_date.setTime(expire_date.getTime() - 1000);
 document.cookie = cookie_keys + "=; expires=" + expire_date.toGMTString();
}

function cookie_init(cookie_keys,selctors){
 var read_cookie_obj        = cookie_read(cookie_keys);
 if(read_cookie_obj.cookie_status==0){
  document.getElementById(selctors).style.display = read_cookie_obj.cookie_value; // CSSセット
 }
}
function save() {                                   // save 関数
 var read_cookie_obj        = cookie_read("box_hold");
 if(read_cookie_obj.cookie_status==0){              // cookieが存在する場合
  cookie_delete("box_hold");                        // cookieの削除
 }
 var display_value = document.getElementById("box").style.display; // ボックスのＣＳＳを求める
 cookie_write("box_hold",display_value,10)                         // cookieの追加
}
window.onload = function(){cookie_init("box_hold","box")}
</script>
<style type="text/css">
body{
}
#fc2_qr_code_header{display:none;}
</style>
</HEAD>
<body>
<p><a href="javascript:void(0)" onclick="show_block();save();"	>非表示/表示</a></p>
<div id="box" style="display:block;" >テスト</div>
</body>
</HTML>
